<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 * Default costumes form for viewing, modifying, adding, etc.
 *  
 * @package com_musashi
 * @author Daniel Torres
 */
 
// Make sure we have access to this page
defined('_JEXEC') or die ('Restricted access. Sorry');
 
// Add the stylesheet for this page
JHTML::_('stylesheet','base.css','components/com_musashi/views/common/');
JHTML::_('stylesheet','default.css','components/com_musashi/views/costumes/tmpl/');
JHTML::_('script','dragdrop.js','components/com_musashi/views/costumes/tmpl/');

// Validate this page. This prevents unknown users from sending forms to our controller objects
$session =& JFactory::getSession();
$session->set('validFormCheck',true);


/**
 *  Checks to see if there is a message from a previous action. Clears the flag if necessary.
 *  This should be moved into a common php include file!
 */
function getActionResult( $actionFlag )
{
    $session =& JFactory::getSession();
    
    // Get previous message, then clear
    $msg = $session->get($actionFlag,'');
    $session->set($actionFlag,'');
    
    return $msg;
}


// We'll be needing this model
$model =& $this->getModel();

// Obtain all clothing types
$clothingTypes = $model->getClothingTypes();


?>
<script language="javascript" type="text/javascript">
<!--

// Validate the group-side list of things to add
function validateList( theList )
{
    theList.form.submit.disabled = false;
}


-->
</script>
<!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->

<musashiContent id="content" class="body">
    
    <header id="title" class="body">
        <div class="name">
            <h1>Your Costumes</h1>
        </div>
        <div class="description">
            <p>Here are the pieces of clothing you own.
            Add or remove items by selecting entire costumes, or by selecting through
            individual pieces. Additional pieces can be added
            through the administration section.</p>
        </div>
    </header>
    
    <?php
    // If there was any error from last attempt, display it here
    if ($lastError != ""){ ?>
        <div class="error"><?php echo $lastError; ?>"</div>
        <?php
    }
    
    // Action result
    if($lastResult != "") {
        echo '<div class="result">'.$lastResult.'</div>';
    }
    ?>
        
    <div id="costume-panel">
        <div class="my-costumes">
            <h2>The costumes I own</h2>
            <h3>These are the costume pieces in my possession</h3>
            <div class="costume-list" id="costumes-drop-target">
                <?php
                    // ---------------------------------------------------------
                    // List all owned pieces
                    // ---------------------------------------------------------
                    if( $clothingTypes )
                    {
                        foreach( $clothingTypes as $type )
                        {
                            // Display this entry, as long as we have something to show
                            $clothingByUser = $model->getClothingByUser( $type[0] );
                            if( $clothingByUser )
                            {                                
                                echo '<h4>'.$type[1].'</h4>';
                                foreach( $clothingByUser as $clothingPiece )                                
                                {
                                    // The random number prevents joomla from caching this operation. We would disable caching altogether for this instead, but since
                                    // this option won't be used that frequently, we don't care.
                                    $removeLink = JRoute::_('index.php?option=com_musashi&controller=costumes&task=removeClothing&itemid='.$clothingPiece[0].'&rn='.rand(1,9999),false);
                                    ?>
                                    <div class="user-item">
                                        <h5><?php echo $clothingPiece[1].' - '.$clothingPiece[2] ?></h5>
                                        <a href="<?php echo  $removeLink; ?>" onclick="return confirm('Removing '+'<?php echo $clothingPiece[1]; ?>'+', are you sure?');">Remove</a>
                                    </div>
                                <?php
                                }                                   
                            }
                        }                            
                    }                        
                ?>
            </div>
        </div>
        <div class="group-costumes">
            <div class="group-parts" id="parts">
                <h2>Group's costume parts</h2>
                <h3>Drag and drop into your owned costumes window</h3>
                <div class="costume-selector">
                    <?php                    
                        // ---------------------------------------------------------
                        // Populate available costume pieces
                        // Group by types. Each type will show its own contents.
                        // ---------------------------------------------------------
                        if ($clothingTypes)
                        {
                            foreach( $clothingTypes as $type )
                            {
                                // Get all the clothe components that belong to this type
                                $clothesByType = $model->getClothingByType( $type[0], true );
                                if ($clothesByType)
                                {
                                    echo '<h4>'.$type[1].'</h4>';
                                    foreach( $clothesByType as $clothingPiece )
                                    {
                                        $label = $clothingPiece[1].' - '.$clothingPiece[2];
                                        ?>
                                        <form class="costume-item" id='clothing-<?php echo $clothingPiece[0]; ?>'
                                                draggable="true"
                                                ondragstart="onStartDragging(event)"
                                                ondragend="onStopDragging(event)"
                                                method="post"
                                                action="<?php echo JRoute::_('index.php'); ?>">
                                            <h5><?php echo $label;?></h5>
                                            <input type="hidden" name="option" value="com_musashi" />
                                            <input type="hidden" name="task" value="addClothing" />
                                            <input type="hidden" name="controller" value="costumes" />
                                            <input type="hidden" name="itemid" value="<?php echo $clothingPiece[0]; ?>" />
                                        </form>
                                    <?php
                                    }
                                }
                                else
                                {
                                    echo '<h4>'.$type[1].'</h4>';
                                    echo '<h6>You have all pieces on this cathegory</h6>';
                                }
                            }
                        }
                    ?>
                </div>
            </div>
            
            
            <!-- Group sets are disabled because it is actually difficult for a member to       -->
            <!-- know, right away, the pieces of clothing assigned to a given set. This kind    -->
            <!-- of selection is better suited for show planning, as a shortcut for assigning   -->
            <!-- known sets of clothing. I'm leaving this here anyways, for reference. If you   -->
            <!-- want to see how it looks, simply uncomment, and set the height of the element  -->
            <!-- costume-selector back to 152 px.                                               -->
            
            <!--
            <div class="group-sets" id="sets">
                <h2>Group's costume sets</h2>
                <h3>Drag and drop into your owned costumes window</h3>
                <div class="costume-selector">
                    <?php
                        // ---------------------------------------------------------
                        // Populate costume sets
                        // ---------------------------------------------------------
                        
                        //$costumes = $model->getCostumes();
                        //foreach( $costumes as $costume )
                        //{?>
                            <form class="costume-item" id="costume-<?php // echo $costume[0]; ?>"
                                                draggable="true"
                                                ondragstart="onStartDragging(event)"
                                                ondragend="onStopDragging(event)"
                                                method="post"
                                                action="<?php //echo JRoute::_('index.php'); ?>">
                                <h5><?php //echo $costume[1]; ?></h5>
                                <?php //if ($costume[2] != "") {
                                    //echo "<h6>".$costume[2]."</h6>";
                                //}?>
                                <input type="hidden" name="option" value="com_musashi" />
                                <input type="hidden" name="task" value="addCostume" />
                                <input type="hidden" name="controller" value="costumes" />
                                <input type="hidden" name="itemid" value="<?php //echo $costume[0];  ?>" />
                            </form>
                        <?php
                        //}
                    ?>
                </div>
            </div>
            -->
        </div>
    </div>

    <!-- Footer. Display syndication, and some copyright information -->
    <footer id="footer" class="body">
        <div class="footer-data">
            <h2>The Musashi Project</h2>
            <h3>A Framework for Managing Your Group</h3>
            <p>(c)2010 - The Musashi Project</p>                    
        </div>
    </footer>

        
</musashiContent>

